﻿using System;
using Framework.MVC;
using Log.Model;
using Identity.Model;
using Billing.Model;
using Identity.IService.Entry;
using Billing.IService.Entry;
using Framework.MVVM.Model;
using Billing.IService;
using Billing.Service;
using Identity.IService;
using Log.IService;
using Common.IService;
using Common.Service;
using Log.Service;
using Identity.Service;


namespace Recharge.Models
{
    [ServiceAttribute(typeof(IRechargeOrderService), typeof(RechargeOrderService), "Instance")]
    [ServiceAttribute(typeof(IIdentityService), typeof(IdentityService), "Instance")]
    [ServiceAttribute(typeof(ILogService), typeof(LogService), "Instance")]
    [ServiceAttribute(typeof(ICodeService), typeof(CodeService), "Instance")]
    public class RechargeOrderQueryPageModel : AsyncModelBase, IWriteLog, IValidateIdentity, IQueryRechargeOrderCount, IQueryRechargeOrderPage
    {
        #region Field

        private const char m_split=';';

        private const string m_empty_identity = "querycge0";
        private const string m_error_identity = "querycge1";
        private const string m_complete_identity = "querycge2";
        private const string m_error_count = "querycge3";
        private const string m_complete_count = "querycge4";
        private const string m_error_query = "querycge5";
        private const string m_complete_query = "querycge6";

        private const string m_count = "count";
        private const string m_page = "page";
        private const string m_size = "size";
        private const string m_list = "list";
        private const string m_resource_user = "u000";

        #endregion Field

        #region AsyncModelBase

        protected override void OnInit(Type type)
        {
            OnBeginRequest();
            this.ValidateIdentity(m_resource_user, m_empty_identity, m_error_identity, m_complete_identity);
        }

        #endregion AsyncModelBase

        #region Write Log

        public void OnWriteLogComplete(string eventCode)
        {
            OnEndRequest();
        }

        public void OnWriteLogError(string eventCode)
        {
            OnEndRequest();
        }

        #endregion Write Log

        #region IValidateIdentity

        public string IdentityCode { get; set; }
        public string Source { get; set; }

        public void OnValidateIdentityComplete(string key, IdentityEntry entry)
        {
            OnBeginRequest();
            this.Log(key, entry);

            RechargeOrderUser = entry.value;
            this.QueryRechargeOrderCount(m_error_count, m_complete_count);
        }

        public void OnValidateIdentityError(string key)
        {
            this.Log(key, new IdentityEntry { code = IdentityCode, source = Source, resource = m_resource_user });
        }

        #endregion IValidateIdentity

        #region IQueryRechargeOrderCount

        public string BeginTime { get; set; }

        public string EndTime { get; set; }

        public string RechargeOrderType { get; set; }

        public string RechargeOrderUser { get; set; }

        public void OnQueryRechargeOrderCountComplete(string key, int entry)
        {
            int start = PageIndex <= 0 ? 1 : PageIndex;
            OnComplete(m_count,entry);
            OnComplete(m_page,start);
            OnComplete(m_size,PageSize);
            if (PageSize > 0 && (start-1) *PageSize<entry)
            {
                OnBeginRequest();
                this.QueryRechargeOrderPage(m_error_query, m_complete_query);
            }
            this.Log(key, entry);
        }

        public void OnQueryRechargeOrderCountError(string key)
        {
            this.Log(key, Source + m_split + RechargeOrderUser);
        }

        #endregion IQueryRechargeOrderCount

        #region IQueryRechargeOrderPage

        public int PageIndex { get; set; }

        public int PageSize { get; set; }

        public void OnQueryRechargeOrderComplete(string key, Billing.IService.Entry.RechargeOrderEntry[] entry)
        {
            OnComplete(m_list,entry);
            this.Log(key, new RechargeOrderQueryParam { user = RechargeOrderUser, type = RechargeOrderType, btime = BeginTime, etime = EndTime });
        }
        public void OnQueryRechargeOrderError(string key)
        {
            this.Log(key, Source + m_split + RechargeOrderUser);
        }

        #endregion IQueryRechargeOrderPage
        
    }
}